package com.stackexchange.webapis.client
{
	import com.adobe.webapis.URLLoaderBase;
	import com.stackexchange.webapis.events.StackExchangeFaultEvent;
	import com.stackexchange.webapis.methodgroups.Answers;
	import com.stackexchange.webapis.methodgroups.Badges;
	import com.stackexchange.webapis.methodgroups.Comments;
	import com.stackexchange.webapis.methodgroups.Errors;
	import com.stackexchange.webapis.methodgroups.MethodCaller;
	import com.stackexchange.webapis.methodgroups.Posts;
	import com.stackexchange.webapis.methodgroups.Questions;
	import com.stackexchange.webapis.methodgroups.ResponseParser;
	import com.stackexchange.webapis.stackexchangeservice_internal;
	
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;

	/**
	 * The StackExchangeService class abstracts the StackExchange API 
	 */
	public class StackExchangeService extends URLLoaderBase
	{
		/**
		 * Const for now, will change it later to support over stack exchange sites
		 * */
		public static const END_POINT:String = "http://api.stackoverflow.com";
		
		/**
		 * API Version
		 **/
		public static const VERSION:String = "1.0";
		
		private var _apiKey:String;
		
		private var _answers:Answers;
		
		private var _badges:Badges;
		
		private var _comments:Comments;
		
		private var _errors:Errors;
		
		private var _posts:Posts;
		
		private var _questions:Questions;
		
		public function StackExchangeService(apiKey:String)
		{
			_apiKey = apiKey;
			
			var methodCaller:MethodCaller = new MethodCaller();
			var responseParser:ResponseParser = new ResponseParser();
			
			_answers = new Answers(this, methodCaller, responseParser);
			_badges = new Badges(this, methodCaller, responseParser);
			_comments = new Comments(this, methodCaller, responseParser);			
			_errors = new Errors(this, methodCaller, responseParser);
			_posts = new Posts(this, methodCaller, responseParser);
			_questions = new Questions(this, methodCaller, responseParser);
		}
		
		/**
		 * API key that should be used to access the StackExchange service.
		 */
		public function get apiKey():String
		{
			return _apiKey;
		}
		
		stackexchangeservice_internal function get urlLoader():URLLoader
		{
			var loader:URLLoader = new URLLoader();
			return loader;
		}
		
		/**
		 * Dispatch an error if a method calls trigger an IOErrorEvent
		 **/
		public function dispatchError(event:IOErrorEvent):void
		{
			var e:StackExchangeFaultEvent = new StackExchangeFaultEvent();
			e.data = event.text;
			dispatchEvent(e);
		}

		/**
		 * Provide read-only access to the Answers method group in the Stack Exchange API
		 */
		public function get answers():Answers
		{
			return _answers;
		}
		
		/**
		 * Provide read-only access to the Badges method group in the Stack Exchange API
		 */
		public function get badges():Badges
		{
			return _badges;
		}

		/**
		 * Provide read-only access to the Comments method group in the Stack Exchange API
		 */
		public function get comments():Comments
		{
			return _comments;
		}
		
		/**
		 * Provide read-only access to the Errors method group in the Stack Exchange API
		 */
		public function get errors():Errors
		{
			return _errors;
		}
		
		/**
		 * Provide read-only access to the Posts method group in the Stack Exchange API
		 */
		public function get posts():Posts
		{
			return _posts;
		}
		
		/**
		 * Provide read-only access to the Questions method group in the Stack Exchange API
		 */
		public function get questions():Questions
		{
			return _questions;
		}


	}
}